package cn.school.demo.ui.article

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentArticleBinding
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.tabs.TabLayoutMediator


class ArticleFragment : Fragment() {

    private lateinit var viewModel: ArticleViewModel
    private lateinit var binding: FragmentArticleBinding
    private lateinit var viewPager: ViewPager2

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        MyApplication.systemBarSetting(MyApplication.LIGHT, requireActivity())
        viewModel =
            ViewModelProvider(this).get(ArticleViewModel::class.java)
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_article, container, false)
        binding.vw = this
        binding.vm = viewModel
        binding.my = MyApplication.Companion
        binding.lifecycleOwner = viewLifecycleOwner
        viewPager = binding.viewpager
        binding.navView.setupWithNavController(findNavController())
        init()
        LogUtils.e(MyApplication.user, MyApplication.user!!.headPortrait)
        return binding.root
    }

    private fun init() {
        viewModel.tabs.observe(viewLifecycleOwner) {
            viewModel.tabs.value?.let {
                MyApplication.tabList.clear()
                it.forEach { hobby ->
                    MyApplication.tabList.add(hobby.hobbyName)
                }
                viewPager.adapter = ViewAdapter(this)
                TabLayoutMediator(
                    binding.mainArticleFragmentTabLayout,
                    viewPager
                ) { tab, position ->
                    tab.text = MyApplication.tabList[position]
                }.attach()
            } ?: LogUtils.e("用户爱好标签请求为空！")
            if (MyApplication.isNotify) {
                MyApplication.pendingIntent?.send()
            }
        }
        //去掉边界阴影
        try {
            val mRecyclerView = binding.viewpager.javaClass.getDeclaredField("mRecyclerView")
            mRecyclerView.isAccessible = true
            (mRecyclerView.get(binding.viewpager) as RecyclerView).overScrollMode =
                View.OVER_SCROLL_NEVER
        } catch (ignore: Exception) {
            ignore.printStackTrace()
        }
    }

    private inner class ViewAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
        override fun getItemCount(): Int = MyApplication.tabList.size

        override fun createFragment(position: Int): Fragment {
            return if (position == 0) ViewPagerFragment.newInstance(position)
            else ViewPager2Fragment.newInstance(position)
        }

    }

    fun gotoSetLove() {
        MyApplication.apply {
            navigateThing["SetLove"] = "Article to SetLove"
            goto(
                R.id.action_Article_to_SetLove,
                findNavController(),
                parentFragmentManager
            )
        }
    }

    fun gotoSearch() = MyApplication.apply {
        goto(
            R.id.action_Article_to_Search,
            findNavController(),
            parentFragmentManager
        )
        navigateThing["Search"] = "Article to Search"
    }
}